clear all;close all;clc
% x_loc = 0:-5:-355;
x_loc = [0:-5:-175 180:-5:5];
y_loc = [-57,-30,-15,0,15,30,45,60,75];

L_or_R = 1;
rad = 0.3;
if_mask=1;

Condition = 1;
if Condition==1
    tar_index = 13; % target is on the left, -60
    noise_index = 61; % noise on the right, +60; we only need to run the model for +60 to get rid of noise!

elseif Condition==2
    tar_index = 4; % target is on the left, -15
    noise_index = 70; % noise on the right, +15; we only need to run the model for +60 to get rid of noise!
    % f1=figure('position',[100 100 900 400]);
elseif Condition==3
    tar_index = 1; % target is on the front center 0,
    noise_index = 72; % noise on the right, +5;
    plot_start=5;
    % elseif Condition==4
    %     tar_index = 56; % target is on the right +85,
    %     noise_index = 55; % noise on the right, +90;  Very bad, but still need to do it and grade.
    % elseif Condition==5
    %     % % % % % % % tar_index = 58; % target is on the right +75,
    %     % % % % % % % noise_index = 52; % noise on the right, +105;
    %     tar_index = 60; % target is on the right +65,
    %     noise_index = 55; % noise on the right, +90;
    % elseif Condition==6
    %     tar_index = 37; % target is on the back +180,
    %     noise_index = 38; % noise on the back, +175;
end


Elevation = 4; % ear level, 4
x_loc(tar_index)
x_loc(noise_index)
pause(0.5)

fs = 32.1e3; % this is the one for the 3D3A database
out_hart = ncread('Acoustic/Subject1/Subject1_HRIRs.sofa',"Data.IR");
igroup = 1
target = [];
noise = [];
randNoise = randperm(25);
for isent = 1:1
    if isent<10
        [ori,fs2]=audioread(['Speech_sound/BEL_SR/BEL_SR0' num2str(igroup) '0' num2str(isent) '.wav']);
    else
        [ori,fs2]=audioread(['Speech_sound/BEL_SR/BEL_SR0' num2str(igroup) num2str(isent) '.wav']);
    end
    target = [target;resample(ori,fs,fs2)];
    if randNoise(isent)<10
        [BB,fs2]=audioread(['Speech_sound/BEL_SR/BEL_SR0' num2str(igroup) '0' num2str(randNoise(isent)) '.wav']);
    else
        [BB,fs2]=audioread(['Speech_sound/BEL_SR/BEL_SR0' num2str(igroup) num2str(randNoise(isent)) '.wav']);
    end
    noise = [noise; BB];
end
noise = [noise; BB];
noise_reversed = flipud(noise);

% add spatial properties
%1))) target
Index = (Elevation - 1)*72+tar_index;%
h_l = squeeze(out_hart(:,1,Index)); % Left HRIRs
h_r = squeeze(out_hart(:,2,Index)); % Right HRIRs
new_tar(:,1) = conv(target,h_l,'same');
new_tar(:,2) = conv(target,h_r,'same');


fsig = (1:length(h_l))./length(h_l).*fs;
both_fft = fft(h_l);
% subplot(222);plot(fsig,abs(both_fft))
% xlim([0 fs/2])

%2))) noise
Index = (Elevation - 1)*72+noise_index;%
h_l = squeeze(out_hart(:,1,Index)); % Left HRIRs
h_r = squeeze(out_hart(:,2,Index)); % Right HRIRs
new_noise(:,1) = conv(noise_reversed,h_l,'same');
new_noise(:,2) = conv(noise_reversed,h_r,'same');
Combined_dual = (new_tar+new_noise(1:length(target),:))./2; % /2 is to make it softer
Combined_mono = sum(Combined_dual,2);
% sound(Combined_mono(1:round(fs*4),:),fs) % play out the original noisy sound
% pause(4)

% ======== Processing
% only do the noise
ifplot = 0;
out_model=f_get_BB_model(h_l,h_r,ifplot,L_or_R);
f_segregation_plot_softhard(new_tar,new_noise,out_model.F_centers,Combined_dual,L_or_R,rad);
% out.freqs(1:200)
% processed_sound = out.reconstructed;
% sound(processed_sound(1:round(fs*4),:),fs) % play out the processed sound
% pause(5)
% audiowrite(['Spatial_sound\saved_Processed_TargetL_Group' num2str(igroup) '_Cond' num2str(Condition) '.wav'],processed_sound,fs)
clear new_noise new_tar

% pause
% if Condition==1
%     exportgraphics(f1, 'BB_Figure3_60deg.png', 'Resolution', 600,'ContentType', 'vector')
% % elseif Condition==2
% %     exportgraphics(gcf, 'AA_Figure4A_15deg.png', 'Resolution', 300,'ContentType', 'vector')
% elseif Condition==3
%     exportgraphics(f1, 'BB_Figure5_15_and_5deg.png', 'Resolution', 600,'ContentType', 'vector')
% end
